from django.shortcuts import render,HttpResponse,redirect
from django.core.paginator import Paginator
from .models import *
from zzy_system.settings import BASE_DIR
import os

# Create your views here.
#作者修改
def updateauthor(request,id):
    if request.method=="GET":
        author=Author.objects.get(id=id)
        return render(request,'update.html',context={'author':author})
    if request.method=="POST":
        name=request.POST.get("name")
        age = request.POST.get("age")
        sex = request.POST.get("sex")

        icon = request.FILES.get("icon")
        with open(os.path.join(BASE_DIR,'static/img',str(icon)),'wb')as f:
            for i in icon.chunks():
                f.write(i)
        author=Author.objects.get(id=id)
        author.name=name
        author.age=age
        author.sex=sex
        author.icon=str(icon)
        author.save()
        return redirect('index')

def index(request):
    author = Author.objects.all()
    page = request.GET.get('page')
    painator = Paginator(author, 3)
    nowpage = painator.get_page(page)
    lastpage = nowpage.number - 1
    nextpage = nowpage.number + 1
    maxpage = painator.num_pages
    return render(request, 'index.html', context={'author':author,'page': page, 'painator': painator, 'nowpage': nowpage,
                                                  'lastpage': lastpage, 'nextpage': nextpage, 'maxpage': maxpage})

def addauthor(request):
    if request.method=="GET":
        return render(request,'addauthor.html')
    if request.method=="POST":
        name=request.POST.get("name")
        age = request.POST.get("age")
        sex = request.POST.get("sex")
        icon = request.FILES.get("icon")
        with open(os.path.join(BASE_DIR,'static/img',str(icon)),'wb')as f:
            for i in icon.chunks():
                f.write(i)
        author=Author(name=name,age=age,sex=sex,icon=str(icon))
        author.save()
        return redirect('index')

def addblog(request):
    if request.method=="GET":
        authora=Author.objects.all()
        return render(request,'addblog.html',context={'authora':authora})
    if request.method=="POST":
        title = request.POST.get("title")
        content = request.POST.get("content")
        btype = request.POST.get("btype")
        author = request.POST.get("author")
        blog=Blog(title=title,content=content,btype=btype,
                  author_id=author)
        blog.save()
        return redirect('index')
def showall(request,id):
    author=Author.objects.filter(id=id).all()
    return render(request,'showall.html',context={'author':author})
def delete(request,id):
    author=Author.objects.get(id=id)
    author.delete()
    return redirect('index')
def blogshow(request,id):
    blog=Blog.objects.filter(author_id=id).all()
    return render(request,'blogshow.html',context={'blog':blog})
def search(request):
    if request.method=="GET":
        return render(request,'index.html')
    if request.method=="POST":
        name=request.POST.get('name')
        author=Author.objects.filter(name__contains=name)
        return render(request,'search.html',{'author':author})
